package cn.dengta.webapp.trade.vo;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;

import cn.dengta.common.model.Status;
import cn.dengta.common.util.Common;
import cn.dengta.common.util.SpringUtil;
import cn.dengta.context.model.WesKit;
import cn.dengta.webapp.trade.entity.WesKitConfig;
import cn.dengta.webapp.trade.service.PhoneBlackListService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Setter
@Getter
@Slf4j
public class WesKitConfigVO {

	/** 总开关 */
	Boolean mainSwitch;

	List<WesKitConfigItemVO> item;

	transient Map<WesKit, WesKitConfigItemVO> map;
	/** 未重新注册的分包 */
	transient List<WesKit> notReRegisterWesKits;

	public static WesKitConfigVO from(Boolean mainSwitch, List<WesKitConfig> configs) {
		Map<WesKit, WesKitConfig> wesKitWesKitConfigMap = configs.stream().collect(Collectors.toMap(e -> WesKit.of(e.getWesKit()), Function.identity()));
		List<WesKitConfigItemVO> itemVOS = Arrays.stream(WesKit.values()).map(kit -> {
			WesKitConfigItemVO itemVO = new WesKitConfigItemVO();
			WesKitConfig config = wesKitWesKitConfigMap.get(kit);
			itemVO.setWesKit(kit);
			if (config == null) {
				List<WesKit> emptyWesKit = Collections.emptyList();
				itemVO.setWomanKits(emptyWesKit);
				itemVO.setManKits(emptyWesKit);
				itemVO.setStatus(0);
				itemVO.setReRegister(0);
			} else {
				itemVO.setWomanKits(convertKits(config.getWomanKits()));
				itemVO.setManKits(convertKits(config.getManKits()));
				itemVO.setStatus(config.getStatus());
				itemVO.setReRegister(config.getReRegister());
			}

			return itemVO;
		}).toList();
		WesKitConfigVO vo = new WesKitConfigVO();
		vo.setItem(itemVOS);
		vo.setMainSwitch(mainSwitch);
		vo.map = itemVOS.stream().collect(Collectors.toMap(e -> e.getWesKit().getWesKit(), Function.identity()));
		return vo;
	}

	private static List<WesKit> convertKits(String kitsStr) {
		List<Integer> kits = Common.splitAsIntList(kitsStr);
		if (kits == null) {
			return Collections.emptyList();
		}
		return kits.stream().map(WesKit::of).collect(Collectors.toList());
	}

	@Nonnull
	public WesKitConfigItemVO getConfig(WesKit wesKit) {
		return map.get(wesKit);
	}

	public boolean isOpen(WesKit wesKit) {
		return mainSwitch && getConfig(wesKit).isOpen();
	}

	public List<WesKit> getSelect(WesKit wesKit, Integer likeGender) {
		return getConfig(wesKit).getSelect(likeGender);
	}

	public boolean isSelectAll(WesKit wesKit, Integer likeGender) {
		return getConfig(wesKit).isSelectAll(likeGender);
	}

	public List<Integer> getSelectValue(WesKit wesKit, Integer likeGender) {
		return getSelect(wesKit, likeGender).stream().map(WesKit::getValue).collect(Collectors.toList());
	}

	public void wrapper(String wesKitColumn, WesKit wesKit, Integer likeGender, QueryWrapper<?> wrapper) {
		if (this.isOpen(wesKit) && !this.isSelectAll(wesKit, likeGender)) {
			log.info("[获取wesKit] {}", JSON.toJSONString(this));
			log.info("[获取wesKit Value] {}", JSON.toJSONString(this.getSelectValue(wesKit, likeGender)));
			log.info("[获取wesKit wesKitColumn] {}", wesKitColumn);
			wrapper.in(wesKitColumn, this.getSelectValue(wesKit, likeGender));
		}
	}

	/**
	 * 获取当前分包可以看到的wesKit
	 * 增加手机号黑名单处理
	 */
	public void wrapper(String wesKitColumn, Long userId, WesKit wesKit, Integer likeGender, QueryWrapper<?> wrapper) {
		// 分包不存在，直接返回
		if (wesKit == null) {
			return;
		}
		// 查看当前用户是否黑名单
		PhoneBlackListService service = SpringUtil.getBean(PhoneBlackListService.class);
		if (userId != null && service != null) {
			boolean blackList = service.isBlackList(userId);
			if (blackList) {
				wrapper.eq(wesKitColumn, wesKit);
				return;
			}
		}
		wrapper(wesKitColumn, wesKit, likeGender, wrapper);
	}

	/**
	 * 获取当前分包可以看到的wesKit
	 * null表示可以查看全部
	 */
	public List<Integer> getCanWesKitValue(WesKit wesKit, Integer likeGender) {
		List<WesKit> canWesKit = getCanWesKit(wesKit, likeGender);
		if (canWesKit == null) {
			return null;
		}
		return canWesKit.stream().map(WesKit::getValue).collect(Collectors.toList());
	}

	public List<WesKit> getCanWesKit(WesKit wesKit, Integer likeGender) {
		if (this.isOpen(wesKit) && !this.isSelectAll(wesKit, likeGender)) {
			return this.getSelect(wesKit, likeGender);
		}
		return null;
	}

	/**
	 * 计算当前分包是否开启可重复注册
	 */
	public List<WesKit> loginWesKit(WesKit wesKit) {
		WesKitConfigItemVO config = getConfig(wesKit);

		boolean isOpen = Status.asBoolean(config.getReRegister());
		if (isOpen) {
			return List.of(wesKit);
		} else {
			return notReRegisterWesKits();
		}
	}

	private List<WesKit> notReRegisterWesKits() {

		if (notReRegisterWesKits != null) {
			return notReRegisterWesKits;
		}

		notReRegisterWesKits = new ArrayList<>();
		for (WesKitConfigItemVO wesKitConfigItemVO : item) {
			if (!Status.asBoolean(wesKitConfigItemVO.getReRegister())) {
				notReRegisterWesKits.add(wesKitConfigItemVO.getWesKit().getWesKit());
			}
		}
		return notReRegisterWesKits;
	}

}
